﻿using System;
using System.Runtime.Caching;
using System.Threading.Tasks;
using Yz.Base;
using Yz.Base.Constants;

namespace Yz.Cache
{
    public class CacheAppProvider
    {
        private readonly MemoryCache _memoryCache = MemoryCache.Default;
        public CacheAppProvider()
        {
            _memoryCache = new MemoryCache(CacheConfig.CachePrefixName + CacheConstant.AppCacheName);
        }

        public CacheAppProvider(string cacheName)
        {
            _memoryCache = new MemoryCache(cacheName);
        }
        public object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration)
        {
            return _memoryCache.AddOrGetExisting(key, value, absoluteExpiration);
        }
        public async Task<object> AddOrGetExistingAsync(string key, object value, DateTimeOffset absoluteExpiration)
        {
            return await Task.Run(() => _memoryCache.AddOrGetExisting(key, value, absoluteExpiration));
        }
        public object Delete(string key)
        {
            return _memoryCache.Remove(key);
        }
        public async Task<object> DeleteAsync(string key)
        {
            return await Task.Run(() => _memoryCache.Remove(key));
        }
        public bool Contains(string key)
        {
            return _memoryCache.Contains(key);
        }

        public async Task<bool> ContainsAsync(string key)
        {
            return await Task.Run(() => _memoryCache.Contains(key));
        }
        public void Set(string key, object value, DateTimeOffset absoluteExpiration)
        {
            _memoryCache.Set(key, value, absoluteExpiration);
        }
        public async Task SetAsync(string key, object value, DateTimeOffset absoluteExpiration)
        {
            await Task.Run(() => _memoryCache.Set(key, value, absoluteExpiration));
        }
        public object Get(string key)
        {
            return _memoryCache.Get(key);
        }
        public async Task<object> GetAsync(string key)
        {
            return await Task.Run(() => _memoryCache.Get(key));
        }

    }
}
